package cn.edu.scau.dormitory_management_system.aop;

import cn.edu.scau.dormitory_management_system.entity.DormRecord;
import cn.edu.scau.dormitory_management_system.entity.StudentEntity;
import cn.edu.scau.dormitory_management_system.module.examine.entity.CheckinApplicationEntity;
import cn.edu.scau.dormitory_management_system.module.examine.entity.CheckinAuditEntity;
import cn.edu.scau.dormitory_management_system.module.examine.entity.CheckoutAuditEntity;
import cn.edu.scau.dormitory_management_system.module.examine.entity.DormAdjustAuditEntity;
import cn.edu.scau.dormitory_management_system.module.examine.service.CheckinApplicationService;
import cn.edu.scau.dormitory_management_system.module.examine.service.CheckoutApplicationService;
import cn.edu.scau.dormitory_management_system.module.examine.service.DormAdjustApplicationService;
import cn.edu.scau.dormitory_management_system.service.DormRecordService;
import cn.edu.scau.dormitory_management_system.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.sql.Wrapper;
import java.util.Date;

/**
 * @author djc
 * @date 2021/8/6
 * @time 11:50
 */

@Aspect
@Component
public class DormRecordHandler {

    @Autowired
    private DormRecordService recordService;

    @Autowired
    private CheckinApplicationService checkinApplicationService;
    @Autowired
    private CheckoutApplicationService checkoutApplicationService;
    @Autowired
    private DormAdjustApplicationService dormAdjustApplicationService;

    @Autowired
    private StudentService studentService;

    @Pointcut("@annotation(cn.edu.scau.dormitory_management_system.aop.annotation.MarkDown)")
    public void pointCut() {
    }

    @Pointcut("@annotation(cn.edu.scau.dormitory_management_system.aop.annotation.ManyDown)")
    public void pointCut2() {
    }

    private String getStuName(String id) {
        QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id", id);
        return studentService.getOne(wrapper).getStuName();
    }

    @AfterReturning(value = "pointCut2()", returning = "val")
    public void manyLog(JoinPoint joinPoint, Object val) {
        if(val.toString().equals("false")) return;
        Object[] args = joinPoint.getArgs();
        DormRecord record = new DormRecord();
        for(Object arg: args) {
            if(arg instanceof String) {
                String stuId = (String) arg;
                record.setStuId(stuId);
                record.setStuName(getStuName(stuId));
                break;
            }
        }
        record.setOperation(0);
        record.setOpTime(new Timestamp(new Date().getTime()));
        recordService.save(record);
    }

    @Before("pointCut()")
    public void addLog(JoinPoint joinPoint) {
        //获取参数
        Object[] args = joinPoint.getArgs();

        DormRecord record = new DormRecord();
        for (Object arg : args) {
            if(arg instanceof CheckinAuditEntity) {
                CheckinAuditEntity entity = (CheckinAuditEntity) arg;
                if(!entity.isPassed()) return; //审核不通过的不记录
                Integer id = entity.getCheckinId();
                String stuId = checkinApplicationService.getById(id).getStuId();

                record.setStuId(stuId);
                record.setStuName(getStuName(stuId));
                break;
            }
            if(arg instanceof DormAdjustAuditEntity) {
                DormAdjustAuditEntity entity = (DormAdjustAuditEntity) arg;
                if(!entity.isPassed()) return; //审核不通过的不记录
                Integer id = entity.getAdjustId();
                String stuId = dormAdjustApplicationService.getById(id).getStuId();

                record.setStuId(stuId);
                record.setStuName(getStuName(stuId));
                break;
            }
            if(arg instanceof CheckoutAuditEntity) {
                CheckoutAuditEntity entity = (CheckoutAuditEntity) arg;
                if(!entity.isPassed()) return;
                Integer id = entity.getCheckoutId();
                String stuId = checkoutApplicationService.getById(id).getStuId();

                record.setStuId(stuId);
                record.setStuName(getStuName(stuId));
                break;
            }
        }

//        0：入宿、1：调宿、2：退宿
        String methodName = joinPoint.getSignature().getName();
        switch (methodName) {
            case "insertInCollegeAudit":
                record.setOperation(0);
                break;
            case "insertAdjustCollegeAudit":
                record.setOperation(1);
                break;
            case "insertOutCollegeAudit":
                record.setOperation(2);
                break;
            default:
                break;
        }

        record.setOpTime(new Timestamp(new Date().getTime()));
        recordService.save(record);
    }
}
